package com.ls.lserp.modules.common.web;

import com.ls.lserp.common.config.Global;
import com.ls.lserp.common.persistence.Page;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.common.web.BaseController;
import com.ls.lserp.modules.common.entity.CommonMaterialList;
import com.ls.lserp.modules.common.entity.OgnizationEntity;
import com.ls.lserp.modules.common.service.CommonMaterialListService;
import com.ls.lserp.modules.material.entity.InventoryRequestEx;
import com.ls.lserp.modules.sys.entity.Brand;
import com.ls.lserp.modules.sys.entity.Dict;
import com.ls.lserp.modules.sys.utils.DictUtils;
import freemarker.template.Configuration;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公用Controller
 * @author yuhui
 * @version 2017-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/common/material")
public class CommonMaterialListController extends BaseController {
	//
	@Autowired
	private CommonMaterialListService commonMaterialListService;
	@Autowired
	Configuration freemarkerConfiguration;

	/**
	 * @Title: 画面跳转用
	 * @return url
	 */
	@RequiresPermissions("material:stock:view")
	@RequestMapping(value = { "list" })
	public String list(Model model,CommonMaterialList commonMaterialList) {
		// 面辅料类别
		List<Dict> styleList2 = DictUtils.getDictList(ResourcesUtil.getConfig("categoryCD"));
		model.addAttribute("materialListCategory", styleList2);
		// 风格
		List<Dict> materialStyle = DictUtils.getDictList(ResourcesUtil.getConfig("material_style"));
		model.addAttribute("materialStyleList", materialStyle);
		// 客户品牌list
		List<Brand> brandList = commonMaterialListService.getBrandList();
		model.addAttribute("brandList",brandList);
        // 客户list
        List<OgnizationEntity> ogizationList = commonMaterialListService.getOgizationList();
        model.addAttribute("ogizationList",ogizationList);
		// 织法
		List<Dict> materialWeaveList = DictUtils.getDictList(ResourcesUtil.getConfig("material_knit_method"));
		model.addAttribute("materialWeaveList", materialWeaveList);
		// 它画面传递检索用参数
		model.addAttribute("commonMaterialItem",commonMaterialList);
		// 画面跳转
		return "common/material/commonMaterialList";
	}

	/**
	 * @Title: 画面检索
	 * @Description: 查询页面
	 * @param commonMaterialList
	 * @param request
	 * @param response
	 * @return searchList
	 * @throws Exception
	 */
	@RequiresPermissions("material:stock:view")
	@RequestMapping(value = { "searchList" })
	@ResponseBody
	public String searchList(CommonMaterialList commonMaterialList, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<Object, Object> listMap = new HashMap<Object, Object>();
		// ajax回调时使用的一览list名
		request.setAttribute(Global.LIST_NAME, "commonMaterialHtml");
		// 拼接字符串供分页使用
		StringBuilder searchParam = new StringBuilder();
		// 面辅料类别
		searchParam.append("&materialTypeCd=");
		searchParam.append(commonMaterialList.getMaterialTypeCd());
		// 客户
		searchParam.append("&venderId=");
		searchParam.append(commonMaterialList.getVenderId());
		// 品牌
		searchParam.append("&brandId=");
		searchParam.append(commonMaterialList.getBrandId());
		// 风格
		searchParam.append("&materialStyle=");
		searchParam.append(commonMaterialList.getMaterialStyle());
		//织法
		searchParam.append("&materialWeave=");
		searchParam.append(commonMaterialList.getMaterialWeave());
		// 物料号
		searchParam.append("&materialCode=");
		searchParam.append(commonMaterialList.getMaterialCode());
		// 放到Attribute里，使用Global类里面的SEARCH_PAEAM作为key
		request.setAttribute(Global.SEARCH_PAEAM, searchParam.toString());

		// 查询一览页面数据
		Page<CommonMaterialList> page = commonMaterialListService.selectfindList(new Page<CommonMaterialList>(request, response),commonMaterialList);
		// 读取系统路径
		// 读取文件生成字符串
		String strUri = request.getContextPath() + Global.getAdminPath();
		String strHtml = "";
		Map<String, Object> map = new HashedMap();
		map.put("adminUri", strUri);
		map.put("materialList", page.getList());
		map.put("pageNo", page.getPageNo());
		map.put("pageSize", page.getPageSize());
		strHtml = FreeMarkerTemplateUtils
				.processTemplateIntoString(freemarkerConfiguration.getTemplate("commonMaterialList.ftl"), map);
		// 自检表总对应的中文名
		for (int i = 0; i<page.getList().size();i++) {
			// 风格
			page.getList().get(i).setMaterialStyleCn(DictUtils.getDictName(page.getList().get(i).getMaterialStyle()));
			// 织法
			page.getList().get(i).setMaterialWeaveCn(DictUtils.getDictName(page.getList().get(i).getMaterialWeave()));
			// 物料类别
			page.getList().get(i).setMaterialTypeCn(DictUtils.getDictName(page.getList().get(i).getMaterialTypeCd()));
			// 单位
			page.getList().get(i).setMaterialUnitCn(DictUtils.getDictName(page.getList().get(i).getMaterialUnitCd()));
		}
		listMap.put("commonMaterialHtml", strHtml);
		listMap.put("materialList", page.getList());
		listMap.put("page", page.getHtml());
		// ajax返回结果
		return renderString(response, listMap);
	}

}
